//
//  VCOfhuancun.m
//  project-A (pianke)
//
//  Created by dllo on 16/3/4.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "VCOfhuancun.h"
#import "CellOfhuancun.h"

#import "VCOfEdit.h"

#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height
@interface VCOfhuancun ()<UITableViewDataSource,UITableViewDelegate>

@property ( nonatomic , retain ) UILabel * cachLabel;

@property (nonatomic,retain)UITableView *tabview;

@property (nonatomic,retain)UILabel *label;


@property (nonatomic,retain)UIView *delete;

@property (nonatomic,retain)UIView *backview;
@end

@implementation VCOfhuancun

-(void)dealloc{
    [_backview release];
    [_delete release];
    [_label release];
    [_tabview release];
    [_cachLabel release];
    [super dealloc];
    
   
    
    
}

- ( long long ) fileSizeAtPath:( NSString *) filePath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if ([manager fileExistsAtPath :filePath]){
        
        return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
        
    }
    
    return 0 ;
    
}

- ( float ) folderSizeAtPath:( NSString *) folderPath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
    
    NSString * fileName;
    
    long long folderSize = 0 ;
    
    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
        
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
        
    }
    
    return folderSize/( 1024.0 * 1024.0 );
    
}


- ( float )filePath

{
    
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    return [ self folderSizeAtPath :cachPath];
    
}

- ( void )clearFile

{
    
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
    
    NSLog ( @"cachpath = %@" , cachPath);
    
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        
        NSString * path = [cachPath stringByAppendingPathComponent :p];
        
        if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
            
            [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
            
        }
        
    }
    
    [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject :nil waitUntilDone : YES ];
    
}

-(void)clearCachSuccess{
    
    
    [self.tabview reloadData];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    self.tabview = [[UITableView alloc]init];
    self.tabview.frame = self.view.bounds;
    self.tabview.backgroundColor = [UIColor clearColor];
    self.tabview.delegate = self;
    self.tabview.dataSource = self;
    self.tabview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tabview];
    [self.tabview
     registerClass:[CellOfhuancun class]
     
     forCellReuseIdentifier:@"pool1"];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CellOfhuancun *cell =  [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    
    
    if(indexPath.row == 0){
        
       
        cell.gongneng.text = @"  修改昵称头像";
        
    }else if (indexPath.row == 1){
        
        cell.gongneng.text = @"  意见反馈";
        
        
    }else if (indexPath.row == 2){
        
        
        cell.gongneng.text = @"   邀请好友使用";
        
    }else if (indexPath.row == 3){
        
        
        cell.gongneng.text = @"   咨询推送设置";
        
    }else if (indexPath.row == 4){
    
         cell.gongneng.text = @"   版本信息";
         cell.xianshi.text = @"3.0.3";
        
    }else if (indexPath.row == 5){
        
        cell.xianshi.text =  [ NSString stringWithFormat : @"约%.2fM" , [ self filePath ]];
        cell.gongneng.text = @"   清除浏览缓存";
        
    }
    
    return cell;
    

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 5) {
        
        self.backview = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [self.view addSubview:self.backview];
        self.backview.backgroundColor = [UIColor blackColor];
        self.backview.alpha = 0.6;
        
        self.delete = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH - 30, HEIGHT * 0.25)];
        self.delete.center = self.view.center;
        [self.view addSubview:self.delete];
        self.delete.backgroundColor = [UIColor whiteColor];
        
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH - 30, HEIGHT * 0.25 *0.4)];
        
        [self.delete addSubview:label];
        
        label.text = @"    杂 游 记";
        
        label.font = [UIFont fontWithName:@"Arial-BoldMT" size:23];
        label.textAlignment = 0;
        label.textColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1];
        
        
        UIView *viewline = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT * 0.1, WIDTH - 29, 1)];
        [self.delete addSubview:viewline];
        viewline.backgroundColor = [UIColor blackColor];
        
        
        
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, HEIGHT * 0.1, WIDTH - 30, HEIGHT * 0.075)];
        [self.delete addSubview:label2];
        
         label2.font = [UIFont fontWithName:@"ArialMT" size:20];
        label2.text = @"   你 确 定 要 清 除 缓 存 吗?";
        label2.textAlignment = 0;
        
        
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        
        button1.frame = CGRectMake(0, HEIGHT * 0.175, (WIDTH - 30) * 0.5, HEIGHT * 0.075);
        [self.delete addSubview:button1];
        [button1 addTarget:self action:@selector(quxiao) forControlEvents:UIControlEventTouchUpInside];
        [button1 setTitle:@"取消" forState:UIControlStateNormal];
        
        [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button1.layer.masksToBounds = YES;
        button1.layer.borderWidth = 1;
        button1.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        
        
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        button2.frame = CGRectMake((WIDTH - 30) * 0.5, HEIGHT * 0.175, (WIDTH - 30) * 0.5, HEIGHT * 0.075);
       
        
        [self.delete addSubview:button2];
        [button2 addTarget:self action:@selector(queding) forControlEvents:UIControlEventTouchUpInside];
        [button2 setTitle:@"确定" forState:UIControlStateNormal];
       [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button2.layer.masksToBounds = YES;
        button2.layer.borderWidth = 1;
        button2.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        
        [viewline release];
        [label release];
        [self.delete release];
        
    }else if (indexPath.row == 0){
        
        VCOfEdit *vc = [[VCOfEdit alloc]init];
        
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
        
    }else if (indexPath.row == 1){
        
        NSLog(@"意见反馈");
        
    }else if (indexPath.row == 2){
        NSLog(@"邀请还有使用");
        
    }else if (indexPath.row == 3){
        NSLog(@"咨询推送设置");
        
    }else if (indexPath.row == 4){
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(0.25 * WIDTH, 0.75 * HEIGHT, WIDTH * 0.5, 50)];
        self.label.text = @"已是最新版本";
        self.label.backgroundColor = [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1];
        self.label.textAlignment = 1;
        self.label.layer.masksToBounds = YES;
        
        self.label.font = [UIFont fontWithName:@"ArialMT" size:23];
        
        self.label.layer.cornerRadius = 5;
        
        self.label.textColor = [UIColor whiteColor];
        [self.view addSubview:self.label];
        [self performSelector:@selector(xiaoshi) withObject:nil afterDelay:1.5];
        
        [self.label release];
        
    }
}


-(void)quxiao{
    
    [self.delete removeFromSuperview];
    [self.backview removeFromSuperview];

}

-(void)queding{
    
    [UIView animateWithDuration:1 animations:^{
         self.backview.alpha = 0;
        self.delete.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        [self.delete removeFromSuperview];
        [self.backview removeFromSuperview];

    }];

    [self clearFile];
}

-(void)xiaoshi{
    
    
 [UIView animateWithDuration:1 animations:^{
     
     self.label.alpha = 0;
     
     
 }completion:^(BOOL finished) {
     
     [self.label removeFromSuperview];
 }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
